import {
  createEntityAdapter,
  createSlice,
  PayloadAction,
} from "@reduxjs/toolkit";
import { AppState } from "@store/index";

// 创建管理访客频道列表的实体适配器
const guestChannelAdapter = createEntityAdapter<Channel>();

export const guestChannelSlice = createSlice({
  name: "guestChannel",
  initialState: guestChannelAdapter.getInitialState(),
  reducers: {
    // 保存访客频道列表
    saveGuestChannels(state, action: PayloadAction<Channel[]>) {
      guestChannelAdapter.setAll(state, action.payload);
    },
    // 删除频道
    removeChannel(state, action: PayloadAction<string>) {
      guestChannelAdapter.removeOne(state, action.payload);
    },
    // 添加访客频道
    addChannel(state, action: PayloadAction<Channel>) {
      guestChannelAdapter.addOne(state, action.payload);
    },
  },
});

export const guestChannelSelectors = guestChannelAdapter.getSelectors<AppState>(
  (state) => state.guestChannel
);

export const { saveGuestChannels, removeChannel, addChannel } =
  guestChannelSlice.actions;
